﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JobbJakt;

namespace JobbJaktWeb
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ButtonLogin_Click(object sender, EventArgs e)
        {

            try
            {
                string username = TextBoxUserName.Text.Trim();
                string password = TextBoxPassWord.Text.Trim();

                if (Users.ValidateLogin(username, password))
                {
                    Session["UserName"] = username.Trim();
                    Users user = Users.GetUserAuthorization(username, password);
                    Session["Authorization"] = user.Authorization;//kolla vilken behörighet och därefter vidarebefordra till diverse sidor
                    string encryptedPassword = EncryptionClass.EncryptString(TextBoxPassWord.Text.Trim());
                    Session["Password"] = encryptedPassword;
                    Response.Redirect("Home.aspx", false);
                }
                else
                {
                    LabelInvalid.Visible = true;

                }
            }
            catch (Exception ex)
            {
                ErrorTable newErrorTable = new ErrorTable() { Message = ex.Message, Type = ex.GetType().ToString() };
                ErrorTable.InsertError(newErrorTable);
                throw new Exception();
            }

        }

        protected void ButtonSignUp_Click(object sender, EventArgs e)
        {
            Response.Redirect("SignUp.aspx");
        }
    }
}